//
//  EFSImportantInformationCollection.m
//  luGongPro
//
//  Created by 王庆学 on 2017/8/24.
//  Copyright © 2017年 王庆学. All rights reserved.
//

#import "EFSImportantInformationCollection.h"
#import <YTXRequest/YTXRequest.h>
#import "TSMessage+YTX.h"


@implementation EFSImportantInformationCollection

+ (instancetype) shared{
    static EFSImportantInformationCollection *collection;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        collection = [[[self class]alloc]init];
    });
    return collection;
}

- (instancetype)init{
    if (self = [super initWithModelClass:[EFSImportantInformationCollection class]]){
        self.remoteSync.urlHookBlock = ^NSURL * _Nonnull{
            return [YTXRequest urlWithName:@"aggregate.list"];
        };
    }
    return self;
}

- (nonnull RACSignal *)fetchremoteOrCache:(NSDictionary *)param{
    RACSubject *subject = [RACSubject subject];
    @weakify(self);
    [[self rac_fetchRemote:param] subscribeNext:^(EFSImportantInformationCollection * collection) {
        @strongify(self);
        [self rac_saveStorage:param];
        [subject sendNext:self];
        [subject sendCompleted];
    } error:^(NSError *error) {
        @strongify(self);
        [TSMessage ytx_showNotificationInViewController:[UIViewController visibleViewController] title:@"网络不给力，请稍后再试" type:TSMessageNotificationTypeError];
        [[self rac_fetchStorage:param] subscribeNext:^(id x) {
            [subject sendNext:self.models.count > 0 ? self : nil];
            [subject sendCompleted];
        }error:^(NSError *error) {
            [subject sendError:error];
        }];
    }];
    return subject;
}

@end
